/**
 * Created by yanghaijin on 2018/5/16.
 */

let Room = require('./../logic/Room');
let MsgCode = require('./../../../define/MsgCode');
let ErrorCode = require('./../../../define/ErrorCode');
let CodeString = require('./../../../define/CodeString');
let MAP_ID = require('./../../../define/MAP_ID');

module.exports = function (app) {
    return new pszRemote(app);
};

let pszRemote = function (app) {
    this.app = app;
    this.channelService = app.get('channelService');
};

/**
 * 创建房间
 * @param {Object} info 玩家信息
 * @param {String} sid server id
 * @param {String} room_id channel name
 * @param {Boolean} flag channel paramete
 * @param {Object} options
 * @param {Function} callback
 */
pszRemote.prototype.createRoom = function (info, sid, room_id, flag, options, callback) {
    let uid = info['uid'];
    let channel = this.channelService.getChannel(room_id, flag);
    console.log(this.app.id2Room);
    if (!!channel) {
        channel.add(uid, sid);
        if (!this.app.id2Room[room_id]) {
            this.app.id2Room[room_id] = new Room(room_id, options, this.channelService, channel);
        }
        this.app.id2Room[room_id].addPlayer(info);
        let palyers = this.app.id2Room[room_id].getPlayers();
        callback(MsgCode.JoinRoom, palyers);
    } else {
        callback(ErrorCode.Room_Nonexistence, []);
    }
};

/**
 * Add user into chat channel.
 * @param {Object} info user info object
 * @param {String} sid server id
 * @param {String} room_id channel name
 * @param {boolean} flag channel paramete
 * @param {Object} options
 * @param {Function} callback
 */
pszRemote.prototype.joinRoom = function (info, sid, room_id, flag, options, callback) {
    let channel = this.channelService.getChannel(room_id, flag);
    if (!!channel) {
        channel.add(info['uid'], sid);
        if (!this.app.id2Room[room_id]) {
            this.app.id2Room[room_id] = new Room(room_id, options);
        }

        if (this.app.id2Room[room_id].addPlayer(info)) {
            console.log('玩家【' + info['uid'] + '】加入房间【' + room_id + '】');
            let palyers = this.app.id2Room[room_id].getPlayers();
            let param = {
                route: 'onMessage',
                msg: {
                    code: MsgCode.JoinRoom,
                    data: {
                        map_id: MAP_ID.SPELL_THREE,
                        room_id: room_id,
                        options: options,
                        players: palyers,
                        msg: CodeString[MsgCode.JoinRoom]
                    }
                }
            };
            channel.pushMessage(param);

            // 如果房间中人数大于1就发牌
            // if (this.app.id2Room[room_id].getPlayerCount() > 1) {
            //     this.app.id2Room[room_id].startGame();
            // }

            callback(MsgCode.JoinRoom, palyers);
        } else {
            console.log('房间【' + room_id + '】已经满员了！');
            callback(ErrorCode.Room_Full, []);
        }
    } else {
        callback(ErrorCode.Room_Nonexistence, []);
    }
};

/**
 * Add user into chat channel.
 * @param {Object} uid 玩家ID
 * @param {String} sid 前端服务器ID
 * @param {String} room_id 信道名称
 * @param {Function} callback 回调函数
 */
pszRemote.prototype.leaveRoom = function (uid, sid, room_id, callback) {
    let channel = this.channelService.getChannel(room_id, false);
    if (!!channel) {
        // 先判断该房间的状态
        let room = this.app.id2Room[room_id];
        if (room && room.canLeaveRoom(uid)) {
            // 1、先把这个人从channel中删除
            channel.leave(uid, sid);
            let param = {
                route: 'onMessage',
                msg: {
                    code: MsgCode.LeaveRoom,
                    data: {
                        uid: uid,
                        room_id: room_id,
                        msg: CodeString[MsgCode.LeaveRoom]
                    }
                }
            };

            // 2、广播该玩家离开放假
            channel.pushMessage(param);

            // 3、从房间对象中删除
            room.removePlayer(uid);
            if (room.canDestroy()) {
                delete this.app.id2Room[room_id];
                this.app.id2Room[room_id] = null;
                this.channelService.destroyChannel(room_id);
                console.log('解散房间【' + room_id + '】');
            }

            // 4、告诉connector玩家成功离开channel
            callback(MsgCode.LeaveRoom);
        }
        // 4、告诉connector玩家成功离开channel
        callback(ErrorCode.LeaveRoom);
    } else {
        callback(ErrorCode.Room_Nonexistence);
    }
};

/**
 * Get user from chat channel.
 *
 * @param {Object} opts parameters for request
 * @param {String} name channel name
 * @param {boolean} flag channel parameter
 * @return {Array} users uids in channel
 *
 */
pszRemote.prototype.get = function (name, flag) {
    var users = [];
    var channel = this.channelService.getChannel(name, flag);
    if (!!channel) {
        users = channel.getMembers();
    }
    for (var i = 0; i < users.length; i++) {
        users[i] = users[i].split('*')[0];
    }
    return users;
};

/**
 * Kick user out chat channel.
 * @param {String} uid unique id for user
 * @param {String} sid server id
 * @param {String} room_id channel name
 * @param {Function} callback
 */
pszRemote.prototype.kick = function (uid, sid, room_id, callback) {
    let channel = this.channelService.getChannel(room_id, false);

    if (!!channel) {
        channel.leave(uid, sid);
    }

    if (channel) {
        let param = {
            route: 'onMessage',
            msg: {
                code: MsgCode.LeaveRoom,
                data: {
                    uid: uid,
                    // msg: CodeString[ErrorCode.LeaveRoom]
                }
            }
        };
        channel.pushMessage(param);
    }
    callback();
};